let app = getApp()
Page({
  /**
   * ҳ��ĳ�ʼ����
   */
  data: {
    index:0,
    isPriceFilterShow: false,
    url:'',
    id:0,
    coursedetail:'',
    seleart:'',
    pageNum:1,
    pageSize:6,
    pages:0,
    classqa:'',
    courserating:'',
    user:'',
    isCollect:false,
    isExpanded:false,
    time:''
  },
  loadCollectState: function(userId, courseId) {
     if (userId && courseId) {
      // 生成唯一存储键
      let collectKey = `collect_${userId}_${courseId}`;
      // 从本地存储读取状态
      let savedCollectState = wx.getStorageSync(collectKey);
      
      // 如果有保存的状态，更新到页面
      if (savedCollectState !== '') {
        this.setData({
          isCollect: savedCollectState
        });
      } else {
        // 如果本地没有存储，可从服务器查询最新状态
        this.checkCollectFromServer(userId, courseId, collectKey);
      }
    }
  },
  
  // 从服务器检查收藏状态（确保与服务器同步）
  checkCollectFromServer: function(userId, courseId, collectKey) {
    wx.request({
      url: this.data.url + 'coursecollect/check/' + courseId + "/" + userId,
      method: 'GET',
      success: resp => {
        if (resp.data.code == 200) {
          // 假设服务器返回{isCollect: true/false}
          this.setData({
            isCollect: resp.data.data.isCollect
          })
          // 同步保存到本地
          wx.setStorageSync(collectKey, resp.data.data.isCollect);
        }
      }
    })
  },
  collectCouse() {
    console.log(this.data.user.id);
    console.log(this.data.id);
    if (this.data.user) {
      let userId = this.data.user.id;
      let courseId = this.data.id;
      // 生成唯一的存储键
      let collectKey = `collect_${userId}_${courseId}`;
      if (!this.data.isCollect) {
        wx.request({
          url: this.data.url + 'coursecollect/add/' + courseId + "/" + userId,
          method: 'GET',
          data: {
            cid: courseId,
            uid: userId
          },
          success: resp => {
            console.log(resp);
            if (resp.data.code == 200) {
              wx.showToast({
                title: '收藏成功',
                icon: 'none'
              })
              this.setData({
                isCollect: true
              })
              // 保存状态到本地存储
              wx.setStorageSync(collectKey, true);
            }
          }
        })
      } else {
        // 取消收藏
        wx.request({
          url: this.data.url + 'coursecollect/canl/' + courseId + "/" + userId,
          method: "DELETE",
          data: {
            cid: courseId,
            uid: userId
          },
          success: resp => {
            if (resp.data.code == 200) {
              wx.showToast({
                title: '取消收藏',
                icon: 'none'
              })
              this.setData({
                isCollect: false
              })
              // 保存状态到本地存储
              wx.setStorageSync(collectKey, false);
            }
          }
        })
      }
    }
  },
  // //////////////////////////////////////////////////////////////
getcourserating(){
  wx.request({
    url:this.data.url+'courserating/page',
    method:'GET',
    data:{
      pageNum:this.data.pageNum,
      pageSize:this.data.pageSize
     },
    success:resp=>{
      this.setData({
        courserating:resp.data.data.list,
        pages:resp.data.data.pages
      })
    }
  })
},
getclassqaquestion(){
  let aa=wx.getStorageSync('user')
  let a1=aa.id
  wx.request({
    url:this.data.url+'classqaquestion/'+a1,
    method:'GET',
    success:resp=>{
      this.setData({
        classqa:resp.data.data
      })
      console.log(this.data.classqa);
    }
  })
},
getseleart(){
 wx.request({
   url:this.data.url+'seleart/page',
   method:'GET',
   data:{
    pageNum:this.data.pageNum,
    pageSize:this.data.pageSize
   },
   success:resp=>{
     this.setData({
      seleart:resp.data.data.list,
      pages:resp.data.data.pages
     })
   }
 })
},
getcoursedetail(){
  wx.request({
    url:this.data.url+'course/'+this.data.id,
    success:resp=>{
    this.setData({
      coursedetail:resp.data.data
     })
    }
  })
},
  changeIndex(e){
    this.setData({
    index:e.currentTarget.dataset.index
    })
  },
  togglePriceFilter(e) {
    const index = e.currentTarget.dataset.index;
    const classqa = [...this.data.classqa]; 
    classqa[index].isExpanded = !classqa[index].isExpanded;
    this.setData({
       classqa 
      });
  },
  // <====================================>
  onLoad(options) {
    let courseId = this.data.id;
    let userId = this.data.user.id;
    if (userId && courseId) {
      // 从本地存储读取状态，使用用户ID+课程ID作为唯一标识
      let collectKey = `collect_${userId}_${courseId}`;
      let savedCollectState = wx.getStorageSync(collectKey);
      // 如果有保存的状态，设置到页面数据中
      if (savedCollectState !== '') {
        this.setData({
          isCollect: savedCollectState
        });
      }
    }
    this.setData({
      id:options.id,
      url:app.globalData.url,
    })
    this.getcoursedetail()
    this.getseleart()
    this.getclassqaquestion()
    this.getcourserating()
  },

  /**
   * �������ں���--����ҳ�������Ⱦ���
   */
  onReady() {

  },

  /**
   * �������ں���--����ҳ����ʾ
   */
  onShow() {
 let user=wx.getStorageSync('user')
 this.setData({
   user:user
 })
  let courseId = this.data.id;
  let userId = this.data.user?.id;
  this.loadCollectState(userId, courseId);
},

  /**
   * �������ں���--����ҳ������
   */
  onHide() {

  },

  /**
   * �������ں���--����ҳ��ж��
   */
  onUnload() {

  },

  /**
   * ҳ������¼���������--�����û���������
   */
  onPullDownRefresh() {

  },

  /**
   * ҳ�����������¼��Ĵ�������
   */
  onReachBottom() {

  },

  /**
   * �û�������ϽǷ���
   */
  onShareAppMessage() {

  }
})
